package com.example.service;


import com.alibaba.fastjson2.JSONObject;
import com.example.entity.BaiduAppId;
import com.example.entity.Connect;
import com.example.utils.BaiduConstant;
import okhttp3.*;

import java.io.IOException;

public class Sample {


    static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();

    public static Connect getConnect() {
        MediaType mediaType = MediaType.parse("application/json");
        BaiduAppId baiduAppId = new BaiduAppId();
        baiduAppId.setApp_id(BaiduConstant.APPID);
        String s = JSONObject.toJSONString(baiduAppId);
        System.out.println(s);
        RequestBody body = RequestBody.create(mediaType, s);
        Request request = new Request.Builder()
            .url("https://qianfan.baidubce.com/v2/app/conversation")
            .method("POST", body)
            .addHeader("Content-Type", "application/json")
            .addHeader("X-Appbuilder-Authorization", BaiduConstant.Authorization)
            .build();
        String string = null;
        try {
            Response response = HTTP_CLIENT.newCall(request).execute();
            string = response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        JSONObject parse = JSONObject.parse(string);
        Connect connect = new Connect();
        connect.setRequest_id(parse.getString("request_id"));
        connect.setConversation_id(parse.getString("conversation_id"));
        System.out.println(connect);
        return connect;
    }
    
    
}